package com.qcby.admin.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qcby.admin.form.CategoryAddForm;
import com.qcby.admin.form.CategoryForm;
import com.qcby.admin.model.SgCategory;
import com.qcby.admin.service.SgCategoryService;
import com.qcby.admin.utils.WebUtils;
import com.qcby.admin.vo.CategoryArticleVO;
import com.qcby.admin.vo.ExcelCategoryVO;
import com.qcby.admin.vo.PageVo;
import com.qcby.framwork.utils.AppHttpCodeEnum;
import com.qcby.framwork.utils.BeanCopyUtils;
import com.qcby.framwork.utils.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 分类表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-09-08
 */
@RestController
@RequestMapping("/content/category")
public class SgCategoryController {
    @Autowired
    private SgCategoryService sgCategoryService;

    /**
     * 文章中的分类查询
     * @return
     */
    @GetMapping("/listAllCategory")
    public ResponseResult getListAllCategory(){
        //返回的结果集
        List<CategoryArticleVO> categoryArticleVOList=new ArrayList<>();
        //只展示状态正常且没有被删除的分类
        QueryWrapper<SgCategory> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("status",0);
        queryWrapper.eq("del_flag",0);
        List<SgCategory> result=sgCategoryService.list(queryWrapper);
        for(SgCategory sgCategory:result){
            CategoryArticleVO categoryArticleVO=new CategoryArticleVO();
            BeanUtils.copyProperties(sgCategory,categoryArticleVO);
            categoryArticleVOList.add(categoryArticleVO);
        }
        return ResponseResult.okResult(categoryArticleVOList);
    }

    /**
     * 分页查询分类列表
     * @return
     */
    @GetMapping("/list")
    public ResponseResult list(CategoryForm categoryForm){
        PageVo pageVo=sgCategoryService.selectCategoryPage(categoryForm);
        return ResponseResult.okResult(pageVo);
    }

    /**
     * 删除分类
     * @param
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public ResponseResult remove(@PathVariable(value = "id")Long[] id) {
        //根据id查询分类信息
        QueryWrapper<SgCategory> queryWrapper=new QueryWrapper<>();
        queryWrapper.in("id",id);
        //mybatisPlus删除
        sgCategoryService.remove(queryWrapper);
        return ResponseResult.okResult();
    }

    /**
     * 添加分类
     * @param categoryDto
     * @return
     */
    @PostMapping
    public ResponseResult add(@RequestBody CategoryAddForm categoryDto){
        SgCategory category = new SgCategory();
        BeanUtils.copyProperties(categoryDto,category);
        Boolean res=sgCategoryService.save(category);
        if(!res){
            return ResponseResult.errorResult(111,"添加失败");
        }
        return ResponseResult.okResult();
    }

    /**
     * 回显
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    //根据分类的id来查询分类
    public ResponseResult getInfo(@PathVariable(value = "id")Long id){
        SgCategory category = sgCategoryService.getById(id);
        return ResponseResult.okResult(category);
    }

    /**
     * 修改分类
     * @param categoryAddForm
     * @return
     */
    @PutMapping
    //根据分类的id来修改分类
    public ResponseResult edit(@RequestBody CategoryAddForm categoryAddForm){
        SgCategory sgCategory=new SgCategory();
        BeanUtils.copyProperties(categoryAddForm,sgCategory);
        //使用mybatisPlus修改分类
        Boolean res=sgCategoryService.updateById(sgCategory);
        if(!res)
            return ResponseResult.errorResult(111,"修改失败");
        return ResponseResult.okResult();
    }

    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response){
        try {
            //查找所有的分类
            List<SgCategory> sgCategories=sgCategoryService.list();
            //放入到导出的ExcelCategory模板中,复制的集合所以使用BeanCopyUtils
            List<ExcelCategoryVO> excelCategoryVOS= BeanCopyUtils.copyBeanList(sgCategories,ExcelCategoryVO.class);
            //设置下载文件的请求头
            WebUtils.setDownLoadHeader("分类.xlsx",response);
            //把数据写入到Excel中
            EasyExcel.write(response.getOutputStream(), ExcelCategoryVO.class).autoCloseStream(Boolean.FALSE).sheet("分类导出")
                    .doWrite(excelCategoryVOS);
        } catch (Exception e) {
            //如果出现异常也要响应json
            ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
            WebUtils.renderString(response, JSON.toJSONString(result));
        }
    }

}

